/**
 * Created by Administrator on 2017/4/1.
 */
function ColorBall(option) {
    this._init(option);
}
ColorBall.prototype = {
    _init: function (option) {
        var option = option || {};
        this.parentId = option.parentId;
        //设置宽度
        this.x = option.x;
        this.y = option.y;
        //设置直径
        this.r = 50;
        //设置背景
        this.bgColor = option.bgColor;
        //设置每次的变化值
        this.dx = _.random(-5, 5);
        this.dy = _.random(-5, 5);
        this.dr = _.random(1, 3);
        //把小球放在数组中
        ballArray.push(this);
    },
    render: function () {
        //创建小球
        var parentNode = document.getElementById(this.parentId);
        var chlidrenNode = document.createElement('div');
        parentNode.appendChild(chlidrenNode);
        //定位
        parentNode.style.position = 'relative';
        chlidrenNode.style.position = 'absolute';
        chlidrenNode.style.left = this.x + 'px';
        chlidrenNode.style.top = this.y + 'px';
        chlidrenNode.style.width = this.r + 'px';
        chlidrenNode.style.height = this.r + 'px';
        chlidrenNode.style.backgroundColor = this.bgColor;
        chlidrenNode.style.borderRadius = '50%';
    },
    update: function () {
        this.x += this.dx;
        this.y += this.dy;
        this.r -= this.dr;
        if (this.r <= 0) {
            this.r = 0;
            ballArray = _.without(ballArray, this);
        }
    }

}
